using Amazon.ECS.Model;
using System;
using RomanJendrusz.Amazon.ETL.Domain;
using System.Collections.Generic;
using RomanJendrusz.Amazon.ETL.Infrastructure;
using RomanJendrusz.Amazon.ETL.Infrastructure.Loging.Core;
using Seller=RomanJendrusz.Amazon.ETL.Domain.Seller;

namespace RomanJendrusz.Amazon.ETL.Messages.MessageMappers
{
    public class GetBookOffersMapper: MessageMapper<GetBookOffersRequest, ItemLookupRequest, ItemLookupResponse>
    {
        public override AmazonResponse MapToAmazonResponse(ItemLookupResponse awsResponse)
        {
            var item = GetRequestItem(awsResponse, 0);
            if(item.IsSetOffers() == false)
            {
                throw new InvalidOperationException("The response doesn have any customer reviews informations");
            }
            var response = new GetBookOffersResponse(CorrelationId);
            response.Offers = new List<SellerOffer>();
            if(item.Offers.IsSetOffer())
            {
                foreach (var offer in item.Offers.Offer)
                {
                    var bookOffer = new SellerOffer();
                    var offerAttributes = AssertThat.IsNotNull(offer.OfferAttributes);
                    bookOffer.Condition = offerAttributes.Condition;
                    bookOffer.CoditionNote = offerAttributes.ConditionNote;
                    if(offer.OfferListing.Count > 0)
                    {
                        var listing = offer.OfferListing[0];
                        bookOffer.Availability = listing.Availability;
                        var price = AssertThat.IsNotNull(listing.Price);
                        bookOffer.Currency = Currency.GetCurrencyFor(price.CurrencyCode);
                        bookOffer.Price = price.Amount;
                        bookOffer.ExchangeId = listing.ExchangeId;
                        bookOffer.IsEligibleForSuperSaverShipping = listing.IsEligibleForSuperSaverShipping;
                        bookOffer.Quantity = Convert.ToInt32(listing.Quantity);
                    }
                    else
                    {
                        LogGateway.LogFor(GetType()).Warn("No informations about the offer listing exists");
                    }
                    if (offer.Seller != null)
                    {
                        bookOffer.Seller = new Seller();
                        bookOffer.Seller.SellerId = AssertThat.IsNotNull(offer.Seller.SellerId);
                    }
                    response.Offers.Add(bookOffer);
                }
            }
            return response;
        }

        public override ItemLookupRequest MapToAwsRequest(GetBookOffersRequest request)
        {
            var awsRequest = new ItemLookupRequest { MerchantId = request.MerchantId };
            awsRequest.ItemId.Add(request.BookAsin);
            awsRequest.OfferPage = request.PageNumber;
            switch (request.Condition)
            {
                case OffersCondition.All:
                    awsRequest.Condition = Condition.All;
                    break;
                case OffersCondition.Collectible:
                    awsRequest.Condition = Condition.Collectible;
                    break;
                case OffersCondition.New:
                    awsRequest.Condition = Condition.New;
                    break;
                case OffersCondition.Refurbished:
                    awsRequest.Condition = Condition.Refurbished;
                    break;
                case OffersCondition.Used:
                    awsRequest.Condition = Condition.Used;
                    break;
            }
            //awsRequest.SearchIndex = "Books";
            //awsRequest.IdType = "ISBN";
            awsRequest.ResponseGroup.Add(request.ResponseGroup);
            awsRequest.MerchantId = request.MerchantId;
            return awsRequest;
        }
    }
}